<?php
namespace app\applet\controller;

use app\applet\model\ElectricityMeter;
use app\applet\model\GasMeter;
use app\applet\model\WaterMeter;
use app\applet\model\UnitBuilding;
use app\manage\model\AdminUser;
use app\manage\model\ApartmentSettings;
use app\manage\model\ContracTmanagement;
use app\manage\model\HousingInformation;
use app\manage\model\Shops;
use think\Controller;
use think\Db;
use think\Request;
use think\Validate;
use think\Model;

class ReadMeter extends Controller {
    public function __construct(Request $request = null)
    {
//        parent::__construct($request);
    }

    /**
     * 抄表管理 V2
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function Meter() {
        $input = input();
        $validate = new Validate([
//            ['tung', 'require', '不能为空'],
//            ['unit', 'require', '不能为空'],
//            ['layer', 'require', '不能为空'],
//            ['status', 'require', '不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $tung = input()['tung'];
        $unit = input()['unit'];
        $layer = input()['layer'];
        $status = input()['status'];
        $admin_id = input()['admin_id'];
        $model_w = new WaterMeter();
        $model_g = new GasMeter();

        $model = new AdminUser();
        $admin_id = $model->findPlatId($admin_id);
        $model = new ElectricityMeter();
        $data = $model->getNowList($admin_id, $tung, $unit, $layer, $status);

        foreach ($data['data'] as &$value) {
            $tmp = $value['status'];
            unset($value['status']);
            $value['status']['electricity'] = $tmp;
            $detail_w = $model_w->detail($value['id']);
            $value['status']['water'] = $detail_w['status'];
            $detail_g = $model_g->detail($value['id']);
            $value['status']['gas'] = $detail_g['status'];
            $value['status']['total'] = $this->getStatus($value['status']['electricity'], $value['status']['water'], $value['status']['gas']);
        }

        $data['todo'] = $model->getTodo($admin_id);

        returnJson(200,"成功", $data)->send();
    }

    /**
     * 抄表 V2
     */
    public function EditMeter() {
        $input = input();
        $validate = new Validate([
//            ['e_num', 'require', '电费不能为空'],
//            ['w_num', 'require', '水费不能为空'],
//            ['g_num', 'require', '气费不能为空'],
            ['id', 'require', 'id不能为空'],
            ['token', 'require', 'token不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $e_num = input()['e_num'];
        $e_image = isset(input()['e_image']) ? input()['e_image'] : [];
        $e_remark = isset(input()['remark']) ? input()['remark'] : '';
        $w_num = input()['w_num'];
        $w_image = isset(input()['w_image']) ? input()['w_image'] : [];
        $w_remark = isset(input()['remark']) ? input()['remark'] : '';
        $g_num = input()['g_num'];
        $g_image = isset(input()['g_image']) ? input()['g_image'] : [];
        $g_remark = isset(input()['remark']) ? input()['remark'] : '';
        $id = input()['id'];
        $token = input()['token'];
//        $status = ($e_num === '' || $w_num === '' || $g_num === '') ? 2 : 1;// 1表示三个均已抄表

        $model_w = new WaterMeter();
        $model_e = new ElectricityMeter();
        $model_g = new GasMeter();
        $data_e['num'] = $e_num;
        $data_e['image'] = empty($e_image) ? '' : implode(',', $e_image);
        $data_e['remark'] = $e_remark;
        $data_e['time'] = time();
        $data_e['meter_id'] = get_user_id($token);
        $data_w['num'] = $w_num;
        $data_w['image'] = empty($w_image) ? '' : implode(',', $w_image);
        $data_w['remark'] = $w_remark;
        $data_w['time'] = time();
        $data_w['meter_id'] = get_user_id($token);
        $data_g['num'] = $g_num;
        $data_g['image'] = empty($g_image) ? '' : implode(',', $g_image);
        $data_g['remark'] = $g_remark;
        $data_g['time'] = time();
        $data_g['meter_id'] = get_user_id($token);

        // 抄表日期在规定有效期内才称之为已抄表
        $model = new ApartmentSettings();
        $model_a = new AdminUser();
        $create_id = get_user_id($token);
        $create_id = $model_a->findPlatId($create_id);
        $setting = $model->index2($create_id);
        // 电费状态
        if ($data_e['num'] === '') {
            $data_e['status'] = 1;
        } else {
            if (date('d') >= $setting['inspection_time'] && date('d') <= $setting['end_inspection']) {
                $data_e['status'] = 2;
            } else {
                $data_e['status'] = 3;
            }
        }
        // 水费状态
        if ($data_w['num'] === '') {
            $data_w['status'] = 1;
        } else {
            if (date('d') >= $setting['inspection_time'] && date('d') <= $setting['end_inspection']) {
                $data_w['status'] = 2;
            } else {
                $data_w['status'] = 3;
            }
        }
        // 气费状态
        if ($data_g['num'] === '') {
            $data_g['status'] = 1;
        } else {
            if (date('d') >= $setting['inspection_time'] && date('d') <= $setting['end_inspection']) {
                $data_g['status'] = 2;
            } else {
                $data_g['status'] = 3;
            }
        }
//        if ($status == 1) {
//            if (date('d') >= $setting['inspection_time'] && date('d') <= $setting['end_inspection']) {
//                $data_e['status'] = 2;
//                $data_w['status'] = 2;
//                $data_g['status'] = 2;
//            } else {
//                $data_e['status'] = 3;
//                $data_w['status'] = 3;
//                $data_g['status'] = 3;
//            }
//        } else {
//            $data_e['status'] = 1;
//            $data_w['status'] = 1;
//            $data_g['status'] = 1;
//        }

        $detail = $model_e->getDetail($id);
        $model_contract = new ContracTmanagement();
        $tenant_info = $model_contract->HouseTenantInfo($detail['house_id'], $detail['type']);
        $data_e['tenant_id'] = $tenant_info['tenant_id'] == null ? '' : $tenant_info['tenant_id'];
        $data_e['tenant_name'] = $tenant_info['tenant_name'] == null ? '' : $tenant_info['tenant_name'];
        $data_e['tenant_tel'] = $tenant_info['tenant_tel'] == null ? '' : $tenant_info['tenant_tel'];
        $data_w['tenant_id'] = $tenant_info['tenant_id'] == null ? '' : $tenant_info['tenant_id'];
        $data_w['tenant_name'] = $tenant_info['tenant_name'] == null ? '' : $tenant_info['tenant_name'];
        $data_w['tenant_tel'] = $tenant_info['tenant_tel'] == null ? '' : $tenant_info['tenant_tel'];
        $data_g['tenant_id'] = $tenant_info['tenant_id'] == null ? '' : $tenant_info['tenant_id'];
        $data_g['tenant_name'] = $tenant_info['tenant_name'] == null ? '' : $tenant_info['tenant_name'];
        $data_g['tenant_tel'] = $tenant_info['tenant_tel'] == null ? '' : $tenant_info['tenant_tel'];

        /**
         * 保留中间数据
         */
//        if ($detail['status'] == 1) {
//            $model_e->m_update($id, $data_e);
//            $model_w->m_update($id, $data_w);
//            $model_g->m_update($id, $data_g);
//            returnJson(200,"成功")->send();
//        } else {
//            $model_e->m_update($id, ['is_newest'=>2]);// 更改上条数据为非最新数据
//            $model_w->m_update($id, ['is_newest'=>2]);// 更改上条数据为非最新数据
//            $model_g->m_update($id, ['is_newest'=>2]);// 更改上条数据为非最新数据
//            $data_e['admin_id'] = $detail['admin_id'];
//            $data_e['house_id'] = $detail['house_id'];
//            $data_e['type'] = $detail['type'];
//            $data_e['tung_num'] = $detail['tung_num'];
//            $data_e['unit_num'] = $detail['unit_num'];
//            $data_e['layer_num'] = $detail['layer_num'];
//            $data_e['room_number'] = $detail['room_number'];
//            $data_e['create_time'] = time();
//            $data_w['admin_id'] = $detail['admin_id'];
//            $data_w['house_id'] = $detail['house_id'];
//            $data_w['type'] = $detail['type'];
//            $data_w['tung_num'] = $detail['tung_num'];
//            $data_w['unit_num'] = $detail['unit_num'];
//            $data_w['layer_num'] = $detail['layer_num'];
//            $data_w['room_number'] = $detail['room_number'];
//            $data_w['create_time'] = time();
//            $data_g['admin_id'] = $detail['admin_id'];
//            $data_g['house_id'] = $detail['house_id'];
//            $data_g['type'] = $detail['type'];
//            $data_g['tung_num'] = $detail['tung_num'];
//            $data_g['unit_num'] = $detail['unit_num'];
//            $data_g['layer_num'] = $detail['layer_num'];
//            $data_g['room_number'] = $detail['room_number'];
//            $data_g['create_time'] = time();
//            $model_e->m_insert($data_e);
//            $model_w->m_insert($data_w);
//            $model_g->m_insert($data_g);
//            returnJson(200,"成功")->send();
//        }

        /**
         * 不保留中间数据
         */
        $model_e->m_update($id, $data_e);
        $model_w->m_update($id, $data_w);
        $model_g->m_update($id, $data_g);
        returnJson(200,"成功")->send();

    }

    /**
     * 抄表详情 V2
     */
    public function DetailMeter() {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '不能为空'],
            ['token', 'require', '不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $id = input()['id'];
        $model_e = new ElectricityMeter();
        $model_w = new WaterMeter();
        $model_g = new GasMeter();
        $data_e = $model_e->getDetail($id);
        $data_w = $model_w->getDetail($id);
        $data_g = $model_g->getDetail($id);

        $data_e['e_num'] = isset($data_e['num']) ? ($data_e['num'] == 0 ? '' : $data_e['num']) : '';
        $data_e['e_image'] = empty($data_e['image']) ? [] : explode(',', $data_e['image']);
        unset($data_e['num']);
        unset($data_e['image']);
        $data_e['w_num'] = isset($data_w['num']) ? ($data_w['num'] == 0 ? '' : $data_w['num']) : '';
        $data_e['w_image'] = empty($data_w['image']) ? [] : explode(',', $data_w['image']);
        $data_e['g_num'] = isset($data_g['num']) ? ($data_g['num'] == 0 ? '' : $data_g['num']) : '';
        $data_e['g_image'] = empty($data_g['image']) ? [] : explode(',', $data_g['image']);

        returnJson(200,"成功", $data_e)->send();
    }

    /**
     * 水表列表
     */
    public function WaterMeter() {
        $input = input();
        $validate = new Validate([
//            ['tung', 'require', '不能为空'],
//            ['unit', 'require', '不能为空'],
//            ['layer', 'require', '不能为空'],
//            ['status', 'require', '不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $tung = input()['tung'];
        $unit = input()['unit'];
        $layer = input()['layer'];
        $status = input()['status'];
        $admin_id = input()['admin_id'];

        $model = new AdminUser();
        $admin_id = $model->findPlatId($admin_id);

        $model = new WaterMeter();
        $data = $model->getNowList($admin_id, $tung, $unit, $layer, $status);
        $model_water = new WaterMeter();
        $data['todo'] = $model_water->getTodo($admin_id);

        returnJson(200,"成功", $data)->send();
    }

    /**
     * 水表抄表
     */
    public function EditWaterMeter() {
        $input = input();
        $validate = new Validate([
            ['num', 'require', '不能为空'],
            ['id', 'require', '不能为空'],
            ['token', 'require', '不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $num = input()['num'];
        $id = input()['id'];
        $token = input()['token'];

        $data['num'] = $num;
        $data['time'] = time();
        $data['meter_id'] = get_user_id($token);
        // 抄表日期在规定有效期内才称之为已抄表
        $model = new ApartmentSettings();
        $model_a = new AdminUser();
        $create_id = get_user_id($token);
        $create_id = $model_a->findPlatId($create_id);
        $setting = $model->index2($create_id);
        if (date('d') >= $setting['inspection_time'] && date('d') <= $setting['end_inspection']) {
            $data['status'] = 2;
        } else {
            $data['status'] = 3;
        }

        $model = new WaterMeter();

        $detail = $model->getDetail($id);
        $model_contract = new ContracTmanagement();
        $tenant_info = $model_contract->HouseTenantInfo($detail['house_id'], $detail['type']);

        if ($detail['status'] == 1) {
            $model->m_update($id, $data);
            $data['tenant_name'] = $tenant_info['tenant_name'] == null ? '' : $tenant_info['tenant_name'];
            $data['tenant_tel'] = $tenant_info['tenant_tel'] == null ? '' : $tenant_info['tenant_tel'];
            returnJson(200,"成功")->send();
        } else {
            $model->m_update($id, ['is_newest'=>2]);// 更改上条数据为非最新数据
            $data['admin_id'] = $detail['admin_id'];
            $data['house_id'] = $detail['house_id'];
            $data['tenant_name'] = $tenant_info['tenant_name'] == null ? '' : $tenant_info['tenant_name'];
            $data['tenant_tel'] = $tenant_info['tenant_tel'] == null ? '' : $tenant_info['tenant_tel'];
            $data['type'] = $detail['type'];
            $data['tung_num'] = $detail['tung_num'];
            $data['unit_num'] = $detail['unit_num'];
            $data['layer_num'] = $detail['layer_num'];
            $data['room_number'] = $detail['room_number'];
            $data['create_time'] = time();
            $model->m_insert($data);
            returnJson(200,"成功")->send();
        }

    }

    /**
     * 水费详情
     */
    public function DetailWaterMeter() {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '不能为空'],
            ['token', 'require', '不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $id = input()['id'];
        $token = input()['token'];

        $model = new WaterMeter();
        $data = $model->getDetail($id);
        $admin_id = get_user_id($token);

        $model = new AdminUser();
        $detail = $model->detail($admin_id, 2);
        $data['name'] = $detail['name'];


        returnJson(200,"成功", $data)->send();
    }

    /**
     * 电费列表
     */
    public function ElectricityMeter() {
        $input = input();
        $validate = new Validate([
//            ['tung', 'require', '不能为空'],
//            ['unit', 'require', '不能为空'],
//            ['layer', 'require', '不能为空'],
//            ['status', 'require', '不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $tung = input()['tung'];
        $unit = input()['unit'];
        $layer = input()['layer'];
        $status = input()['status'];
        $admin_id = input()['admin_id'];

        $model = new AdminUser();
        $admin_id = $model->findPlatId($admin_id);

        $model = new ElectricityMeter();
        $data = $model->getNowList($admin_id, $tung, $unit, $layer, $status);
        $model_water = new ElectricityMeter();
        $data['todo'] = $model_water->getTodo($admin_id);

        returnJson(200,"成功", $data)->send();
    }

    /**
     * 电费详情
     */
    public function DetailElectricityMeter() {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '不能为空'],
            ['token', 'require', '不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $id = input()['id'];
        $token = input()['token'];

        $model = new ElectricityMeter();
        $data = $model->getDetail($id);
        $admin_id = get_user_id($token);

        $model = new AdminUser();
        $detail = $model->detail($admin_id, 2);
        $data['name'] = $detail['name'];

        returnJson(200,"成功", $data)->send();
    }

    /**
     * 电费抄表
     */
    public function EditElectricityMeter() {
        $input = input();
        $validate = new Validate([
            ['num', 'require', '不能为空'],
            ['id', 'require', '不能为空'],
            ['token', 'require', '不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $num = input()['num'];
        $id = input()['id'];
        $token = input()['token'];

        $data['num'] = $num;
        $data['time'] = time();
        $data['meter_id'] = get_user_id($token);
        // 抄表日期在规定有效期内才称之为已抄表
        $model = new ApartmentSettings();
        $model_a = new AdminUser();
        $create_id = get_user_id($token);
        $create_id = $model_a->findPlatId($create_id);
        $setting = $model->index2($create_id);
        if (date('d') >= $setting['inspection_time'] && date('d') <= $setting['end_inspection']) {
            $data['status'] = 2;
        } else {
            $data['status'] = 3;
        }

        $model = new ElectricityMeter();
        $detail = $model->getDetail($id);

        $model_contract = new ContracTmanagement();
        $tenant_info = $model_contract->HouseTenantInfo($detail['house_id'], $detail['type']);
        if ($detail['status'] == 1) {
            $model->m_update($id, $data);
            $data['tenant_name'] = $tenant_info['tenant_name'] == null ? '' : $tenant_info['tenant_name'];
            $data['tenant_tel'] = $tenant_info['tenant_tel'] == null ? '' : $tenant_info['tenant_tel'];
            returnJson(200,"成功")->send();
        } else {
            $model->m_update($id, ['is_newest'=>2]);// 更改上条数据为非最新数据
            $data['admin_id'] = $detail['admin_id'];
            $data['house_id'] = $detail['house_id'];
            $data['tenant_name'] = $tenant_info['tenant_name'] == null ? '' : $tenant_info['tenant_name'];
            $data['tenant_tel'] = $tenant_info['tenant_tel'] == null ? '' : $tenant_info['tenant_tel'];
            $data['type'] = $detail['type'];
            $data['tung_num'] = $detail['tung_num'];
            $data['unit_num'] = $detail['unit_num'];
            $data['layer_num'] = $detail['layer_num'];
            $data['room_number'] = $detail['room_number'];
            $data['create_time'] = time();
            $model->m_insert($data);
            returnJson(200,"成功")->send();
        }
    }

    /**
     * 气费列表
     */
    public function GasMeter() {
        $input = input();
        $validate = new Validate([
//            ['tung', 'require', '不能为空'],
//            ['unit', 'require', '不能为空'],
//            ['layer', 'require', '不能为空'],
//            ['status', 'require', '不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $tung = input()['tung'];
        $unit = input()['unit'];
        $layer = input()['layer'];
        $status = input()['status'];
        $admin_id = input()['admin_id'];

        $model = new AdminUser();
        $admin_id = $model->findPlatId($admin_id);

        $model = new GasMeter();
        $data = $model->getNowList($admin_id, $tung, $unit, $layer, $status);
        $model_water = new GasMeter();
        $data['todo'] = $model_water->getTodo($admin_id);

        returnJson(200,"成功", $data)->send();
    }

    /**
     * 气费抄表
     */
    public function EditGasMeter() {
        $input = input();
        $validate = new Validate([
            ['num', 'require', '不能为空'],
            ['id', 'require', '不能为空'],
            ['token', 'require', '不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $num = input()['num'];
        $id = input()['id'];
        $token = input()['token'];

        $data['num'] = $num;
        $data['time'] = time();
        $data['meter_id'] = get_user_id($token);
        // 抄表日期在规定有效期内才称之为已抄表
        $model = new ApartmentSettings();
        $model_a = new AdminUser();
        $create_id = get_user_id($token);
        $create_id = $model_a->findPlatId($create_id);
        $setting = $model->index2($create_id);
        if (date('d') >= $setting['inspection_time'] && date('d') <= $setting['end_inspection']) {
            $data['status'] = 2;
        } else {
            $data['status'] = 3;
        }

        $model = new GasMeter();

        $detail = $model->getDetail($id);
        $model_contract = new ContracTmanagement();
        $tenant_info = $model_contract->HouseTenantInfo($detail['house_id'], $detail['type']);

        if ($detail['status'] == 1) {
            $model->m_update($id, $data);
            $data['tenant_name'] = $tenant_info['tenant_name'] == null ? '' : $tenant_info['tenant_name'];
            $data['tenant_tel'] = $tenant_info['tenant_tel'] == null ? '' : $tenant_info['tenant_tel'];
            returnJson(200,"成功")->send();
        } else {
            $model->m_update($id, ['is_newest'=>2]);// 更改上条数据为非最新数据
            $data['admin_id'] = $detail['admin_id'];
            $data['house_id'] = $detail['house_id'];
            $data['tenant_name'] = $tenant_info['tenant_name'] == null ? '' : $tenant_info['tenant_name'];
            $data['tenant_tel'] = $tenant_info['tenant_tel'] == null ? '' : $tenant_info['tenant_tel'];
            $data['type'] = $detail['type'];
            $data['tung_num'] = $detail['tung_num'];
            $data['unit_num'] = $detail['unit_num'];
            $data['layer_num'] = $detail['layer_num'];
            $data['room_number'] = $detail['room_number'];
            $data['create_time'] = time();
            $model->m_insert($data);
            returnJson(200,"成功")->send();
        }
    }

    /**
     * 气费详情
     */
    public function DetailGasMeter() {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '不能为空'],
            ['token', 'require', '不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $id = input()['id'];
        $token = input()['token'];

        $model = new GasMeter();
        $data = $model->getDetail($id);
        $admin_id = get_user_id($token);

        $model = new AdminUser();
        $detail = $model->detail($admin_id, 2);
        $data['name'] = $detail['name'];

        returnJson(200,"成功", $data)->send();
    }

    /**
     * 获取单元楼号与楼层
     */
    public function HouseUnit() {
        $model = new UnitBuilding();
        $detail = $model->unitIndex();
        $data = [];

        foreach ($detail['data'] as $k => $datum) {
            $data[$k]['unit_id'] = $datum['unit_id'];
            $data[$k]['tung_num'] = $datum['tung_num'];
            $data[$k]['unit_num'] = $datum['unit_num'];
            $data[$k]['layer_num'] = $datum['layer_num'];
        }

        returnJson(200,"成功", $data)->send();
    }

    /**
     * 获取单元楼号与楼层
     */
    public function HouseUnitTwo() {
        $input = input();
        $validate = new Validate([
            ['id', 'require', 'id不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $id = $input['id'];
        $model = new AdminUser();
        $id = $model->findPlatId($id);
        $model = new UnitBuilding();
        $detail = $model->unitIndex2($id);
        $data = [];

        foreach ($detail['data'] as $k => $datum) {
            $data[$datum['tung_num'] . '-' . $datum['unit_num']] = [];
            for ($i = 1; $i <= $datum['layer_num']; $i++) {
                array_push($data[$datum['tung_num'] . '-' . $datum['unit_num']], $i);
            }
        }

        returnJson(200,"成功", $data)->send();
    }

    /**
     * 待办事项
     */
    public function Todo() {
        $input = input();
        $validate = new Validate([
            ['admin_id', 'require', '不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }

        $admin_id = input()['admin_id'];

        $model = new AdminUser();
        $admin_id = $model->findPlatId($admin_id);

        $model_electricity = new ElectricityMeter();
        $model_water = new WaterMeter();
        $model_gas = new GasMeter();

        $data['electricity'] = $model_electricity->getTodo($admin_id);
        $data['water'] = $model_water->getTodo($admin_id);
        $data['gas'] = $model_gas->getTodo($admin_id);

        returnJson(200,"成功", $data)->send();
    }

    /**
     * 每月产生抄表记录
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function genarateMeter() {
        $model_house = new HousingInformation();
        $model_shop = new Shops();
        $model_electricity = new ElectricityMeter();
        $model_water = new WaterMeter();
        $model_gas = new GasMeter();
        $model_contractManage = new ContracTmanagement();
        $time = time();

        /**
         * 获取住房信息
         */
        $house_list = $model_house->indexCron();
        foreach ($house_list as &$house) {
            $house['admin_id'] = $house['create_id'];
            $house['type'] = 1;
            $house['house_id'] = $house['housing_id'];
            $house['tung_num'] = $house['h_tung_num'];
            $house['unit_num'] = $house['h_unit_num'];
            $house['layer_num'] = $house['h_layer_num'];
            $house['room_number'] = $house['h_room_number'];
            $house['create_time'] = $time;
            $house['status'] = 1;
            $house['num'] = 0.00;
            $house['time'] = 0;
            unset($house['h_agent']);
            unset($house['create_id']);
            unset($house['housing_id']);
            unset($house['h_tung_num']);
            unset($house['h_unit_num']);
            unset($house['h_layer_num']);
            unset($house['h_room_number']);
        }

        /**
         * 获取商铺信息
         */
        $shop_list = $model_shop->indexCron();
        foreach ($shop_list as &$shop) {
            $shop['admin_id'] = $shop['create_id'];
            $shop['type'] = 2;
            $shop['house_id'] = $shop['shops_id'];
            $shop['tung_num'] = $shop['s_tung_num'];
            $shop['unit_num'] = $shop['s_unit_num'];
            $shop['layer_num'] = $shop['s_layer_num'];
            $shop['room_number'] = $shop['s_room_number'];
            $shop['create_time'] = $time;
            $shop['status'] = 1;
            $shop['num'] = 0.00;
            $shop['time'] = 0;
            unset($shop['s_agent']);
            unset($shop['create_id']);
            unset($shop['shops_id']);
            unset($shop['s_tung_num']);
            unset($shop['s_unit_num']);
            unset($shop['s_layer_num']);
            unset($shop['s_room_number']);
        }

        /**
         * 合并信息
         */
        $house_infos = array_merge_recursive($house_list, $shop_list);

        /**
         * 获取租客信息
         */

        foreach ($house_infos as &$house) {
            $data = $model_contractManage->HouseTenantInfo($house['house_id'], $house['type']);
            $house['tenant_id'] = $data['tenant_id'] === null ? '' : $data['tenant_id'];
            $house['tenant_name'] = $data['tenant_name'] === null ? '' : $data['tenant_name'];
            $house['tenant_tel'] = $data['tenant_tel'] === null ? '' : $data['tenant_tel'];
        }

        /**
         * 水电气分别插表
         */
        foreach ($house_infos as $house_info) {
            $house_info['is_newest'] = 1;
            $model_electricity->m_insert($house_info);
            $model_water->m_insert($house_info);
            $model_gas->m_insert($house_info);
        }
    }


    //ping一个IP地址，能不能通
//    function ping($ip)
//    {
//        ob_start();
//        while(1){
//            system("ping $ip", $list);
//            for($i=0;$i<count($list);$i++){
//                print $list[$i]."\n";
//            }
//            ob_flush();
//            flush();
//            sleep(1);
//        }
//    }

    public function test()
    {
        $ip = 'my.oschina.net';//IP地址
        ($this->ping($ip));

    }
    public function ping($ip,$times=4)
    {
        $info = array();
        if(!is_numeric($times) ||  $times-4<0)
        {
            $times = 4;
        }
        if (PATH_SEPARATOR==':' || DIRECTORY_SEPARATOR=='/')//linux
        {
            exec("ping $ip -c $times",$info);
            if (count($info) < 9)
            {
                $info['error']='timeout';
            }
        }
        else //windows
        {
            exec("ping $ip -n $times",$info);
            if (count($info) < 10)
            {
                $info['error']='timeout';
            }
        }
        returnJson(200,"成功", $info)->send();
    }

    /**
     * 根据水电气三个状态综合判断最终状态，1表示未抄表，2表示已抄表，3表示补录
     * @param $status1
     * @param $status2
     * @param $status3
     * @return int
     */
    private function getStatus($status1, $status2, $status3) {
        if ($status1 == 3 || $status2 == 3 || $status3 == 3) {
            return 3;
        } elseif ($status1 == 2 && $status2 == 2 && $status3 == 2) {
            return 2;
        } else {
            return 1;
        }
    }
}